package mr.fileToDb;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Writable;

import utils.LangUtils;

public class EsbMRBean implements Writable {
	public static short rsrpValue = -999;
	public static short rsrqValue = -99;
	public long imsi;//�������
	public int mmeUeS1apId = 0;
	public int enodebId = 0;
	public short ci;
	public short pci;
	public short rsrp;
	public short rsrq;
	public float longitude;
	public float latitude;
	public boolean period;
	public long time = 0;
	public String eventId;
	public String ncells = "";

	public String cellId;//�����
	public int gridId;//դ��Id
	public int sceneId;//������Id

	@Override
	public void readFields(DataInput in) throws IOException {
		sceneId = in.readInt();
		gridId = in.readInt();
		imsi = in.readLong();
		mmeUeS1apId = in.readInt();
		enodebId = in.readInt();
		ci = in.readShort();
		pci = in.readShort();
		rsrp = in.readShort();
		rsrq = in.readShort();
		longitude = in.readFloat();
		latitude = in.readFloat();
		period = in.readBoolean();
		time = in.readLong();
		eventId = in.readUTF();
		ncells = in.readUTF();
	}

	@Override
	public void write(DataOutput data) throws IOException {
		data.writeInt(sceneId);
		data.writeInt(gridId);
		data.writeLong(imsi);
		data.writeInt(mmeUeS1apId);
		data.writeInt(enodebId);
		data.writeShort(ci);
		data.writeShort(pci);
		data.writeShort(rsrp);
		data.writeShort(rsrq);
		data.writeFloat(longitude);
		data.writeFloat(latitude);
		data.writeBoolean(period);
		data.writeLong(time);
		data.writeUTF(eventId==null?"":eventId);
		data.writeUTF(ncells==null?"":ncells);
	}

	/**
	 * @param rsrq
	 * @return int
	 */
	public int getRsrp(long rsrp) {
		return (int) (-140 + rsrp);
	}

	public String getKey() {
		return sceneId + "_" + gridId;
	}

	public void initGridKpi(GridKpiBean kpiBean) {
		kpiBean.totalmrs++;
		if (rsrq != rsrpValue) {
			kpiBean.avgrsrp_n++;
			int nowrsrp = getRsrp(rsrp);
			kpiBean.avgrsrp_v += nowrsrp;
			if (nowrsrp < -110) {
				kpiBean.weakCoverageMR++;
			}
		}
	}
	public void initSceneKpi(SceneKpiBean kpiBean) {
		kpiBean.totalmrs++;
		if (rsrq != rsrpValue) {
			kpiBean.avgrsrp_n++;
			int nowrsrp = getRsrp(rsrp);
			kpiBean.avgrsrp_v += nowrsrp;
			if (nowrsrp < -110) {
				kpiBean.weakCoverageMR++;
			}
		}
	}

	public void parse(String[] cols) {
		int idx = 0;
		this.sceneId = LangUtils.toInt(cols[idx++], 0);
		this.gridId = LangUtils.toInt(cols[idx++], 0);
		this.imsi = LangUtils.toLong(cols[idx++], 0);
		this.mmeUeS1apId = LangUtils.toInt(cols[idx++], 0);
		this.enodebId = LangUtils.toInt(cols[idx++], 0);
		this.time = LangUtils.toLong(cols[idx++]);
		this.ci = LangUtils.toShort(cols[idx++], (short) 0);
		this.pci = LangUtils.toShort(cols[idx++], (short) 0);
		this.rsrp = LangUtils.toShort(cols[idx++], (short) 0);
		this.rsrq = LangUtils.toShort(cols[idx++], (short) 0);

		this.longitude = LangUtils.toFloat(cols[idx++], (short) 0);
		this.latitude = LangUtils.toFloat(cols[idx++], (short) 0);
		this.period = LangUtils.toBoolean(cols[idx++], false);

		this.eventId = cols[idx++];
		this.ncells = cols[idx++];

	}

	public static String toHeader() {
		final StringBuilder sb = new StringBuilder();
		sb.append("sceneId,");
		sb.append("gridId,");
		sb.append("imsi,");
		sb.append("mmeUeS1apId,");
		sb.append("enodebId,");
		sb.append("time,");
		sb.append("ci,");
		sb.append("pci,");
		sb.append("rsrp,");
		sb.append("rsrq,");
		sb.append("longitude,");
		sb.append("latitude,");
		sb.append("period,");
		sb.append("eventId,");
		sb.append("ncells,");
		return sb.toString();
	}

	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder();
		sb.append(sceneId).append(",");
		sb.append(gridId).append(",");
		sb.append(imsi).append(",");
		sb.append(mmeUeS1apId).append(",");
		sb.append(enodebId).append(",");
		sb.append(time).append(",");
		sb.append(ci).append(",");
		sb.append(pci).append(",");
		sb.append(rsrp).append(",");
		sb.append(rsrq).append(",");
		sb.append(longitude).append(",");
		sb.append(latitude).append(",");
		sb.append(period).append(",");
		sb.append(eventId).append(",");
		sb.append(ncells);
		return sb.toString();
	}

	public String getEnodebIdCellid() {
		return enodebId + "_" + ci;
	}

	public Object getEnodebidPCI() {
		return enodebId + "_" + pci;
	}

}
